import { getToken, setToken, removeToken, setUserInfor, getUserInfor, removeUserInfor } from '@/utils/auth'
import { login } from '@/api/user'

const state = {
  token: getToken(),
  userInfor: getUserInfor()
}
const actions = {
  logout(context) {
    context.commit('removeToken')
  },
  async login(context, payload) {
    const { data } = await login(payload)
    // console.log(data)
    if (data.code === 200) {
      context.commit('updataToken', data.data.token)
      context.commit('userInfor', data.data.user)
    }
    return data
  }
}
const mutations = {
  updataToken(state, payload) {
    // console.log(payload.token)
    state.token = payload.token
    setToken(payload.token)
  },
  userInfor(state, payload) {
    setUserInfor(payload)
  },
  removeToken(state) {
    state.token = ''
    state.userInfor = ''
    removeUserInfor()
    removeToken()
  }

}

export default {
  namespaced: true,
  state,
  actions,
  mutations
}
